<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>4.节流throttle</title>
  <style>
    #div1 {
      width: 300px;
      height: 300px;
      border: 3px solid #000;
    }
  </style>
</head>
<body>

  <div id="div1" draggable="true">可拖拽</div>

  <script>
    
    const div1 = document.getElementById('div1')
    // div1.addEventListener('drag', function(e){
    //   console.log(e.offsetX, e.offsetY);
    // } )

    // 节流函数throttle
    function throttle(fn, delay) {

      let timer = null

      return function() {
        if (timer) {
          return 
        }

        timer = setTimeout(() => {
          fn.apply(this, arguments)
          timer = null
        }, delay)
      }
    }

    div1.addEventListener('drag', throttle(function(e) {
      console.log(e.offsetX, e.offsetY);
    }, 500) )

  </script>
</body>
</html>